package org.leaf.green.b2b

class MarketTypeController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : '/b2b/market/type/list', model : [marketTypeInstanceList: MarketType.list(params), marketTypeInstanceTotal: MarketType.count()]
    }

    def create = {
        def marketTypeInstance = new MarketType()
        marketTypeInstance.properties = params
        render view : '/b2b/market/type/create', model : [marketTypeInstance: marketTypeInstance]
    }

    def save = {
        def marketTypeInstance = new MarketType(params)
        if (!marketTypeInstance.hasErrors() && marketTypeInstance.save()) {
            flash.message = "marketType.created"
            flash.args = [marketTypeInstance.id]
            flash.defaultMessage = "MarketType ${marketTypeInstance.id} created"
            redirect(action: "show", id: marketTypeInstance.id)
        }
        else {
            render(view: "/b2b/market/type/create", model: [marketTypeInstance: marketTypeInstance])
        }
    }

    def show = {
        def marketTypeInstance = MarketType.get(params.id)
        if (!marketTypeInstance) {
            flash.message = "marketType.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketType not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/market/type/show', model : [marketTypeInstance: marketTypeInstance]
        }
    }

    def edit = {
        def marketTypeInstance = MarketType.get(params.id)
        if (!marketTypeInstance) {
            flash.message = "marketType.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketType not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/market/type/edit', model : [marketTypeInstance: marketTypeInstance]
        }
    }

    def update = {
        def marketTypeInstance = MarketType.get(params.id)
        if (marketTypeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (marketTypeInstance.version > version) {
                    
                    marketTypeInstance.errors.rejectValue("version", "marketType.optimistic.locking.failure", "Another user has updated this MarketType while you were editing")
                    render(view: "/b2b/market/type/edit", model: [marketTypeInstance: marketTypeInstance])
                    return
                }
            }
            marketTypeInstance.properties = params
            if (!marketTypeInstance.hasErrors() && marketTypeInstance.save()) {
                flash.message = "marketType.updated"
                flash.args = [params.id]
                flash.defaultMessage = "MarketType ${params.id} updated"
                redirect(action: "show", id: marketTypeInstance.id)
            }
            else {
                render(view: "/b2b/market/type/edit", model: [marketTypeInstance: marketTypeInstance])
            }
        }
        else {
            flash.message = "marketType.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketType not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def marketTypeInstance = MarketType.get(params.id)
        if (marketTypeInstance) {
            try {
                marketTypeInstance.delete()
                flash.message = "marketType.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "MarketType ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "marketType.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "MarketType ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "marketType.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketType not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
